home *** CD-ROM | disk | FTP | other *** search
/ Ultra Pack / UltraComputing Partner Applications.iso / SunLabs / tclTK / src / tcl7.4 / tclParse.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-18  |  36.8 KB  |  1,321 lines

  1. /* 
  2.  * tclParse.c --
  3.  *
  4.  *    This file contains a collection of procedures that are used
  5.  *    to parse Tcl commands or parts of commands (like quoted
  6.  *    strings or nested sub-commands).
  7.  *
  8.  * Copyright (c) 1987-1993 The Regents of the University of California.
  9.  * Copyright (c) 1994-1995 Sun Microsystems, Inc.
  10.  *
  11.  * See the file "license.terms" for information on usage and redistribution
  12.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  13.  */
  14.  
  15. #ifndef lint
  16. static char sccsid[] = "@(#) tclParse.c 1.44 95/04/18 11:30:00";
  17. #endif
  18.  
  19. #include "tclInt.h"
  20. #include "tclPort.h"
  21.  
  22. /*
  23.  * The following table assigns a type to each character.  Only types
  24.  * meaningful to Tcl parsing are represented here.  The table is
  25.  * designed to be referenced with either signed or unsigned characters,
  26.  * so it has 384 entries.  The first 128 entries correspond to negative
  27.  * character values, the next 256 correspond to positive character
  28.  * values.  The last 128 entries are identical to the first 128.  The
  29.  * table is always indexed with a 128-byte offset (the 128th entry
  30.  * corresponds to a 0 character value).
  31.  */
  32.  
  33. char tclTypeTable[] = {
  34.     /*
  35.      * Negative character values, from -128 to -1:
  36.      */
  37.  
  38.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  39.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  40.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  41.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  42.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  43.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  44.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  45.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  46.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  47.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  48.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  49.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  50.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  51.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  52.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  53.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  54.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  55.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  56.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  57.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  58.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  59.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  60.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  61.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  62.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  63.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  64.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  65.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  66.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  67.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  68.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  69.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  70.  
  71.     /*
  72.      * Positive character values, from 0-127:
  73.      */
  74.  
  75.     TCL_COMMAND_END,   TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  76.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  77.     TCL_NORMAL,        TCL_SPACE,         TCL_COMMAND_END,   TCL_SPACE,
  78.     TCL_SPACE,         TCL_SPACE,         TCL_NORMAL,        TCL_NORMAL,
  79.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  80.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  81.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  82.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  83.     TCL_SPACE,         TCL_NORMAL,        TCL_QUOTE,         TCL_NORMAL,
  84.     TCL_DOLLAR,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  85.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  86.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  87.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  88.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  89.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_COMMAND_END,
  90.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  91.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  92.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  93.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  94.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  95.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  96.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  97.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_OPEN_BRACKET,
  98.     TCL_BACKSLASH,     TCL_COMMAND_END,   TCL_NORMAL,        TCL_NORMAL,
  99.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  100.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  101.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  102.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  103.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  104.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  105.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_OPEN_BRACE,
  106.     TCL_NORMAL,        TCL_CLOSE_BRACE,   TCL_NORMAL,        TCL_NORMAL,
  107.  
  108.     /*
  109.      * Large unsigned character values, from 128-255:
  110.      */
  111.  
  112.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  113.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  114.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  115.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  116.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  117.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  118.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  119.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  120.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  121.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  122.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  123.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  124.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  125.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  126.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  127.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  128.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  129.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  130.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  131.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  132.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  133.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  134.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  135.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  136.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  137.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  138.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  139.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  140.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  141.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  142.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  143.     TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,        TCL_NORMAL,
  144. };
  145.  
  146. /*
  147.  * Function prototypes for procedures local to this file:
  148.  */
  149.  
  150. static char *    QuoteEnd _ANSI_ARGS_((char *string, int term));
  151. static char *    VarNameEnd _ANSI_ARGS_((char *string));
  152.  
  153. /*
  154.  *----------------------------------------------------------------------
  155.  *
  156.  * Tcl_Backslash --
  157.  *
  158.  *    Figure out how to handle a backslash sequence.
  159.  *
  160.  * Results:
  161.  *    The return value is the character that should be substituted
  162.  *    in place of the backslash sequence that starts at src.  If
  163.  *    readPtr isn't NULL then it is filled in with a count of the
  164.  *    number of characters in the backslash sequence.
  165.  *
  166.  * Side effects:
  167.  *    None.
  168.  *
  169.  *----------------------------------------------------------------------
  170.  */
  171.  
  172. char
  173. Tcl_Backslash(src, readPtr)
  174.     char *src;            /* Points to the backslash character of
  175.                  * a backslash sequence. */
  176.     int *readPtr;        /* Fill in with number of characters read
  177.                  * from src, unless NULL. */
  178. {
  179.     register char *p = src+1;
  180.     char result;
  181.     int count;
  182.  
  183.     count = 2;
  184.  
  185.     switch (*p) {
  186.     case 'a':
  187.         result = 0x7;    /* Don't say '\a' here, since some compilers */
  188.         break;        /* don't support it. */
  189.     case 'b':
  190.         result = '\b';
  191.         break;
  192.     case 'f':
  193.         result = '\f';
  194.         break;
  195.     case 'n':
  196.         result = '\n';
  197.         break;
  198.     case 'r':
  199.         result = '\r';
  200.         break;
  201.     case 't':
  202.         result = '\t';
  203.         break;
  204.     case 'v':
  205.         result = '\v';
  206.         break;
  207.     case 'x':
  208.         if (isxdigit(UCHAR(p[1]))) {
  209.         char *end;
  210.  
  211.         result = strtoul(p+1, &end, 16);
  212.         count = end - src;
  213.         } else {
  214.         count = 2;
  215.         result = 'x';
  216.         }
  217.         break;
  218.     case '\n':
  219.         do {
  220.         p++;
  221.         } while (isspace(UCHAR(*p)));
  222.         result = ' ';
  223.         count = p - src;
  224.         break;
  225.     case 0:
  226.         result = '\\';
  227.         count = 1;
  228.         break;
  229.     default:
  230.         if (isdigit(UCHAR(*p))) {
  231.         result = *p - '0';
  232.         p++;
  233.         if (!isdigit(UCHAR(*p))) {
  234.             break;
  235.         }
  236.         count = 3;
  237.         result = (result << 3) + (*p - '0');
  238.         p++;
  239.         if (!isdigit(UCHAR(*p))) {
  240.             break;
  241.         }
  242.         count = 4;
  243.         result = (result << 3) + (*p - '0');
  244.         break;
  245.         }
  246.         result = *p;
  247.         count = 2;
  248.         break;
  249.     }
  250.  
  251.     if (readPtr != NULL) {
  252.     *readPtr = count;
  253.     }
  254.     return result;
  255. }
  256.  
  257. /*
  258.  *--------------------------------------------------------------
  259.  *
  260.  * TclParseQuotes --
  261.  *
  262.  *    This procedure parses a double-quoted string such as a
  263.  *    quoted Tcl command argument or a quoted value in a Tcl
  264.  *    expression.  This procedure is also used to parse array
  265.  *    element names within parentheses, or anything else that
  266.  *    needs all the substitutions that happen in quotes.
  267.  *
  268.  * Results:
  269.  *    The return value is a standard Tcl result, which is
  270.  *    TCL_OK unless there was an error while parsing the
  271.  *    quoted string.  If an error occurs then interp->result
  272.  *    contains a standard error message.  *TermPtr is filled
  273.  *    in with the address of the character just after the
  274.  *    last one successfully processed;  this is usually the
  275.  *    character just after the matching close-quote.  The
  276.  *    fully-substituted contents of the quotes are stored in
  277.  *    standard fashion in *pvPtr, null-terminated with
  278.  *    pvPtr->next pointing to the terminating null character.
  279.  *
  280.  * Side effects:
  281.  *    The buffer space in pvPtr may be enlarged by calling its
  282.  *    expandProc.
  283.  *
  284.  *--------------------------------------------------------------
  285.  */
  286.  
  287. int
  288. TclParseQuotes(interp, string, termChar, flags, termPtr, pvPtr)
  289.     Tcl_Interp *interp;        /* Interpreter to use for nested command
  290.                  * evaluations and error messages. */
  291.     char *string;        /* Character just after opening double-
  292.                  * quote. */
  293.     int termChar;        /* Character that terminates "quoted" string
  294.                  * (usually double-quote, but sometimes
  295.                  * right-paren or something else). */
  296.     int flags;            /* Flags to pass to nested Tcl_Eval calls. */
  297.     char **termPtr;        /* Store address of terminating character
  298.                  * here. */
  299.     ParseValue *pvPtr;        /* Information about where to place
  300.                  * fully-substituted result of parse. */
  301. {
  302.     register char *src, *dst, c;
  303.  
  304.     src = string;
  305.     dst = pvPtr->next;
  306.  
  307.     while (1) {
  308.     if (dst == pvPtr->end) {
  309.         /*
  310.          * Target buffer space is about to run out.  Make more space.
  311.          */
  312.  
  313.         pvPtr->next = dst;
  314.         (*pvPtr->expandProc)(pvPtr, 1);
  315.         dst = pvPtr->next;
  316.     }
  317.  
  318.     c = *src;
  319.     src++;
  320.     if (c == termChar) {
  321.         *dst = '\0';
  322.         pvPtr->next = dst;
  323.         *termPtr = src;
  324.         return TCL_OK;
  325.     } else if (CHAR_TYPE(c) == TCL_NORMAL) {
  326.         copy:
  327.         *dst = c;
  328.         dst++;
  329.         continue;
  330.     } else if (c == '$') {
  331.         int length;
  332.         char *value;
  333.  
  334.         value = Tcl_ParseVar(interp, src-1, termPtr);
  335.         if (value == NULL) {
  336.         return TCL_ERROR;
  337.         }
  338.         src = *termPtr;
  339.         length = strlen(value);
  340.         if ((pvPtr->end - dst) <= length) {
  341.         pvPtr->next = dst;
  342.         (*pvPtr->expandProc)(pvPtr, length);
  343.         dst = pvPtr->next;
  344.         }
  345.         strcpy(dst, value);
  346.         dst += length;
  347.         continue;
  348.     } else if (c == '[') {
  349.         int result;
  350.  
  351.         pvPtr->next = dst;
  352.         result = TclParseNestedCmd(interp, src, flags, termPtr, pvPtr);
  353.         if (result != TCL_OK) {
  354.         return result;
  355.         }
  356.         src = *termPtr;
  357.         dst = pvPtr->next;
  358.         continue;
  359.     } else if (c == '\\') {
  360.         int numRead;
  361.  
  362.         src--;
  363.         *dst = Tcl_Backslash(src, &numRead);
  364.         dst++;
  365.         src += numRead;
  366.         continue;
  367.     } else if (c == '\0') {
  368.         Tcl_ResetResult(interp);
  369.         sprintf(interp->result, "missing %c", termChar);
  370.         *termPtr = string-1;
  371.         return TCL_ERROR;
  372.     } else {
  373.         goto copy;
  374.     }
  375.     }
  376. }
  377.  
  378. /*
  379.  *--------------------------------------------------------------
  380.  *
  381.  * TclParseNestedCmd --
  382.  *
  383.  *    This procedure parses a nested Tcl command between
  384.  *    brackets, returning the result of the command.
  385.  *
  386.  * Results:
  387.  *    The return value is a standard Tcl result, which is
  388.  *    TCL_OK unless there was an error while executing the
  389.  *    nested command.  If an error occurs then interp->result
  390.  *    contains a standard error message.  *TermPtr is filled
  391.  *    in with the address of the character just after the
  392.  *    last one processed;  this is usually the character just
  393.  *    after the matching close-bracket, or the null character
  394.  *    at the end of the string if the close-bracket was missing
  395.  *    (a missing close bracket is an error).  The result returned
  396.  *    by the command is stored in standard fashion in *pvPtr,
  397.  *    null-terminated, with pvPtr->next pointing to the null
  398.  *    character.
  399.  *
  400.  * Side effects:
  401.  *    The storage space at *pvPtr may be expanded.
  402.  *
  403.  *--------------------------------------------------------------
  404.  */
  405.  
  406. int
  407. TclParseNestedCmd(interp, string, flags, termPtr, pvPtr)
  408.     Tcl_Interp *interp;        /* Interpreter to use for nested command
  409.                  * evaluations and error messages. */
  410.     char *string;        /* Character just after opening bracket. */
  411.     int flags;            /* Flags to pass to nested Tcl_Eval. */
  412.     char **termPtr;        /* Store address of terminating character
  413.                  * here. */
  414.     register ParseValue *pvPtr;    /* Information about where to place
  415.                  * result of command. */
  416. {
  417.     int result, length, shortfall;
  418.     Interp *iPtr = (Interp *) interp;
  419.  
  420.     iPtr->evalFlags = flags | TCL_BRACKET_TERM;
  421.     result = Tcl_Eval(interp, string);
  422.     *termPtr = iPtr->termPtr;
  423.     if (result != TCL_OK) {
  424.     /*
  425.      * The increment below results in slightly cleaner message in
  426.      * the errorInfo variable (the close-bracket will appear).
  427.      */
  428.  
  429.     if (**termPtr == ']') {
  430.         *termPtr += 1;
  431.     }
  432.     return result;
  433.     }
  434.     (*termPtr) += 1;
  435.     length = strlen(iPtr->result);
  436.     shortfall = length + 1 - (pvPtr->end - pvPtr->next);
  437.     if (shortfall > 0) {
  438.     (*pvPtr->expandProc)(pvPtr, shortfall);
  439.     }
  440.     strcpy(pvPtr->next, iPtr->result);
  441.     pvPtr->next += length;
  442.     Tcl_FreeResult(iPtr);
  443.     iPtr->result = iPtr->resultSpace;
  444.     iPtr->resultSpace[0] = '\0';
  445.     return TCL_OK;
  446. }
  447.  
  448. /*
  449.  *--------------------------------------------------------------
  450.  *
  451.  * TclParseBraces --
  452.  *
  453.  *    This procedure scans the information between matching
  454.  *    curly braces.
  455.  *
  456.  * Results:
  457.  *    The return value is a standard Tcl result, which is
  458.  *    TCL_OK unless there was an error while parsing string.
  459.  *    If an error occurs then interp->result contains a
  460.  *    standard error message.  *TermPtr is filled
  461.  *    in with the address of the character just after the
  462.  *    last one successfully processed;  this is usually the
  463.  *    character just after the matching close-brace.  The
  464.  *    information between curly braces is stored in standard
  465.  *    fashion in *pvPtr, null-terminated with pvPtr->next
  466.  *    pointing to the terminating null character.
  467.  *
  468.  * Side effects:
  469.  *    The storage space at *pvPtr may be expanded.
  470.  *
  471.  *--------------------------------------------------------------
  472.  */
  473.  
  474. int
  475. TclParseBraces(interp, string, termPtr, pvPtr)
  476.     Tcl_Interp *interp;        /* Interpreter to use for nested command
  477.                  * evaluations and error messages. */
  478.     char *string;        /* Character just after opening bracket. */
  479.     char **termPtr;        /* Store address of terminating character
  480.                  * here. */
  481.     register ParseValue *pvPtr;    /* Information about where to place
  482.                  * result of command. */
  483. {
  484.     int level;
  485.     register char *src, *dst, *end;
  486.     register char c;
  487.  
  488.     src = string;
  489.     dst = pvPtr->next;
  490.     end = pvPtr->end;
  491.     level = 1;
  492.  
  493.     /*
  494.      * Copy the characters one at a time to the result area, stopping
  495.      * when the matching close-brace is found.
  496.      */
  497.  
  498.     while (1) {
  499.     c = *src;
  500.     src++;
  501.     if (dst == end) {
  502.         pvPtr->next = dst;
  503.         (*pvPtr->expandProc)(pvPtr, 20);
  504.         dst = pvPtr->next;
  505.         end = pvPtr->end;
  506.     }
  507.     *dst = c;
  508.     dst++;
  509.     if (CHAR_TYPE(c) == TCL_NORMAL) {
  510.         continue;
  511.     } else if (c == '{') {
  512.         level++;
  513.     } else if (c == '}') {
  514.         level--;
  515.         if (level == 0) {
  516.         dst--;            /* Don't copy the last close brace. */
  517.         break;
  518.         }
  519.     } else if (c == '\\') {
  520.         int count;
  521.  
  522.         /*
  523.          * Must always squish out backslash-newlines, even when in
  524.          * braces.  This is needed so that this sequence can appear
  525.          * anywhere in a command, such as the middle of an expression.
  526.          */
  527.  
  528.         if (*src == '\n') {
  529.         dst[-1] = Tcl_Backslash(src-1, &count);
  530.         src += count - 1;
  531.         } else {
  532.         (void) Tcl_Backslash(src-1, &count);
  533.         while (count > 1) {
  534.                     if (dst == end) {
  535.                         pvPtr->next = dst;
  536.                         (*pvPtr->expandProc)(pvPtr, 20);
  537.                         dst = pvPtr->next;
  538.                         end = pvPtr->end;
  539.                     }
  540.             *dst = *src;
  541.             dst++;
  542.             src++;
  543.             count--;
  544.         }
  545.         }
  546.     } else if (c == '\0') {
  547.         Tcl_SetResult(interp, "missing close-brace", TCL_STATIC);
  548.         *termPtr = string-1;
  549.         return TCL_ERROR;
  550.     }
  551.     }
  552.  
  553.     *dst = '\0';
  554.     pvPtr->next = dst;
  555.     *termPtr = src;
  556.     return TCL_OK;
  557. }
  558.  
  559. /*
  560.  *--------------------------------------------------------------
  561.  *
  562.  * TclParseWords --
  563.  *
  564.  *    This procedure parses one or more words from a command
  565.  *    string and creates argv-style pointers to fully-substituted
  566.  *    copies of those words.
  567.  *
  568.  * Results:
  569.  *    The return value is a standard Tcl result.
  570.  *    
  571.  *    *argcPtr is modified to hold a count of the number of words
  572.  *    successfully parsed, which may be 0.  At most maxWords words
  573.  *    will be parsed.  If 0 <= *argcPtr < maxWords then it
  574.  *    means that a command separator was seen.  If *argcPtr
  575.  *    is maxWords then it means that a command separator was
  576.  *    not seen yet.
  577.  *
  578.  *    *TermPtr is filled in with the address of the character
  579.  *    just after the last one successfully processed in the
  580.  *    last word.  This is either the command terminator (if
  581.  *    *argcPtr < maxWords), the character just after the last
  582.  *    one in a word (if *argcPtr is maxWords), or the vicinity
  583.  *    of an error (if the result is not TCL_OK).
  584.  *    
  585.  *    The pointers at *argv are filled in with pointers to the
  586.  *    fully-substituted words, and the actual contents of the
  587.  *    words are copied to the buffer at pvPtr.
  588.  *
  589.  *    If an error occurrs then an error message is left in
  590.  *    interp->result and the information at *argv, *argcPtr,
  591.  *    and *pvPtr may be incomplete.
  592.  *
  593.  * Side effects:
  594.  *    The buffer space in pvPtr may be enlarged by calling its
  595.  *    expandProc.
  596.  *
  597.  *--------------------------------------------------------------
  598.  */
  599.  
  600. int
  601. TclParseWords(interp, string, flags, maxWords, termPtr, argcPtr, argv, pvPtr)
  602.     Tcl_Interp *interp;        /* Interpreter to use for nested command
  603.                  * evaluations and error messages. */
  604.     char *string;        /* First character of word. */
  605.     int flags;            /* Flags to control parsing (same values as
  606.                  * passed to Tcl_Eval). */
  607.     int maxWords;        /* Maximum number of words to parse. */
  608.     char **termPtr;        /* Store address of terminating character
  609.                  * here. */
  610.     int *argcPtr;        /* Filled in with actual number of words
  611.                  * parsed. */
  612.     char **argv;        /* Store addresses of individual words here. */
  613.     register ParseValue *pvPtr;    /* Information about where to place
  614.                  * fully-substituted word. */
  615. {
  616.     register char *src, *dst;
  617.     register char c;
  618.     int type, result, argc;
  619.     char *oldBuffer;        /* Used to detect when pvPtr's buffer gets
  620.                  * reallocated, so we can adjust all of the
  621.                  * argv pointers. */
  622.  
  623.     src = string;
  624.     oldBuffer = pvPtr->buffer;
  625.     dst = pvPtr->next;
  626.     for (argc = 0; argc < maxWords; argc++) {
  627.     argv[argc] = dst;
  628.  
  629.     /*
  630.      * Skip leading space.
  631.      */
  632.     
  633.     skipSpace:
  634.     c = *src;
  635.     type = CHAR_TYPE(c);
  636.     while (type == TCL_SPACE) {
  637.         src++;
  638.         c = *src;
  639.         type = CHAR_TYPE(c);
  640.     }
  641.     
  642.     /*
  643.      * Handle the normal case (i.e. no leading double-quote or brace).
  644.      */
  645.  
  646.     if (type == TCL_NORMAL) {
  647.         normalArg:
  648.         while (1) {
  649.         if (dst == pvPtr->end) {
  650.             /*
  651.              * Target buffer space is about to run out.  Make
  652.              * more space.
  653.              */
  654.     
  655.             pvPtr->next = dst;
  656.             (*pvPtr->expandProc)(pvPtr, 1);
  657.             dst = pvPtr->next;
  658.         }
  659.     
  660.         if (type == TCL_NORMAL) {
  661.             copy:
  662.             *dst = c;
  663.             dst++;
  664.             src++;
  665.         } else if (type == TCL_SPACE) {
  666.             goto wordEnd;
  667.         } else if (type == TCL_DOLLAR) {
  668.             int length;
  669.             char *value;
  670.     
  671.             value = Tcl_ParseVar(interp, src, termPtr);
  672.             if (value == NULL) {
  673.             return TCL_ERROR;
  674.             }
  675.             src = *termPtr;
  676.             length = strlen(value);
  677.             if ((pvPtr->end - dst) <= length) {
  678.             pvPtr->next = dst;
  679.             (*pvPtr->expandProc)(pvPtr, length);
  680.             dst = pvPtr->next;
  681.             }
  682.             strcpy(dst, value);
  683.             dst += length;
  684.         } else if (type == TCL_COMMAND_END) {
  685.             if ((c == ']') && !(flags & TCL_BRACKET_TERM)) {
  686.             goto copy;
  687.             }
  688.  
  689.             /*
  690.              * End of command;  simulate a word-end first, so
  691.              * that the end-of-command can be processed as the
  692.              * first thing in a new word.
  693.              */
  694.  
  695.             goto wordEnd;
  696.         } else if (type == TCL_OPEN_BRACKET) {
  697.             pvPtr->next = dst;
  698.             result = TclParseNestedCmd(interp, src+1, flags, termPtr,
  699.                 pvPtr);
  700.             if (result != TCL_OK) {
  701.             return result;
  702.             }
  703.             src = *termPtr;
  704.             dst = pvPtr->next;
  705.         } else if (type == TCL_BACKSLASH) {
  706.             int numRead;
  707.     
  708.             *dst = Tcl_Backslash(src, &numRead);
  709.  
  710.             /*
  711.              * The following special check allows a backslash-newline
  712.              * to be treated as a word-separator, as if the backslash
  713.              * and newline had been collapsed before command parsing
  714.              * began.
  715.              */
  716.  
  717.             if (src[1] == '\n') {
  718.             src += numRead;
  719.             goto wordEnd;
  720.             }
  721.             src += numRead;
  722.             dst++;
  723.         } else {
  724.             goto copy;
  725.         }
  726.         c = *src;
  727.         type = CHAR_TYPE(c);
  728.         }
  729.     } else {
  730.     
  731.         /*
  732.          * Check for the end of the command.
  733.          */
  734.     
  735.         if (type == TCL_COMMAND_END) {
  736.         if (flags & TCL_BRACKET_TERM) {
  737.             if (c == '\0') {
  738.             Tcl_SetResult(interp, "missing close-bracket",
  739.                 TCL_STATIC);
  740.             return TCL_ERROR;
  741.             }
  742.         } else {
  743.             if (c == ']') {
  744.             goto normalArg;
  745.             }
  746.         }
  747.         goto done;
  748.         }
  749.     
  750.         /*
  751.          * Now handle the special cases: open braces, double-quotes,
  752.          * and backslash-newline.
  753.          */
  754.  
  755.         pvPtr->next = dst;
  756.         if (type == TCL_QUOTE) {
  757.         result = TclParseQuotes(interp, src+1, '"', flags,
  758.             termPtr, pvPtr);
  759.         } else if (type == TCL_OPEN_BRACE) {
  760.         result = TclParseBraces(interp, src+1, termPtr, pvPtr);
  761.         } else if ((type == TCL_BACKSLASH) && (src[1] == '\n')) {
  762.         /*
  763.          * This code is needed so that a backslash-newline at the
  764.          * very beginning of a word is treated as part of the white
  765.          * space between words and not as a space within the word.
  766.          */
  767.  
  768.         src += 2;
  769.         goto skipSpace;
  770.         } else {
  771.         goto normalArg;
  772.         }
  773.         if (result != TCL_OK) {
  774.         return result;
  775.         }
  776.     
  777.         /*
  778.          * Back from quotes or braces;  make sure that the terminating
  779.          * character was the end of the word.
  780.          */
  781.     
  782.         c = **termPtr;
  783.         if ((c == '\\') && ((*termPtr)[1] == '\n')) {
  784.         /*
  785.          * Line is continued on next line;  the backslash-newline
  786.          * sequence turns into space, which is OK.  No need to do
  787.          * anything here.
  788.          */
  789.         } else {
  790.         type = CHAR_TYPE(c);
  791.         if ((type != TCL_SPACE) && (type != TCL_COMMAND_END)) {
  792.             if (*src == '"') {
  793.             Tcl_SetResult(interp,
  794.                 "extra characters after close-quote",
  795.                 TCL_STATIC);
  796.             } else {
  797.             Tcl_SetResult(interp,
  798.                 "extra characters after close-brace",
  799.                 TCL_STATIC);
  800.             }
  801.             return TCL_ERROR;
  802.         }
  803.         }
  804.         src = *termPtr;
  805.         dst = pvPtr->next;
  806.     }
  807.  
  808.     /*
  809.      * We're at the end of a word, so add a null terminator.  Then
  810.      * see if the buffer was re-allocated during this word.  If so,
  811.      * update all of the argv pointers.
  812.      */
  813.  
  814.     wordEnd:
  815.     *dst = '\0';
  816.     dst++;
  817.     if (oldBuffer != pvPtr->buffer) {
  818.         int i;
  819.  
  820.         for (i = 0; i <= argc; i++) {
  821.         argv[i] = pvPtr->buffer + (argv[i] - oldBuffer);
  822.         }
  823.         oldBuffer = pvPtr->buffer;
  824.     }
  825.     }
  826.  
  827.     done:
  828.     pvPtr->next = dst;
  829.     *termPtr = src;
  830.     *argcPtr = argc;
  831.     return TCL_OK;
  832. }
  833.  
  834. /*
  835.  *--------------------------------------------------------------
  836.  *
  837.  * TclExpandParseValue --
  838.  *
  839.  *    This procedure is commonly used as the value of the
  840.  *    expandProc in a ParseValue.  It uses malloc to allocate
  841.  *    more space for the result of a parse.
  842.  *
  843.  * Results:
  844.  *    The buffer space in *pvPtr is reallocated to something
  845.  *    larger, and if pvPtr->clientData is non-zero the old
  846.  *    buffer is freed.  Information is copied from the old
  847.  *    buffer to the new one.
  848.  *
  849.  * Side effects:
  850.  *    None.
  851.  *
  852.  *--------------------------------------------------------------
  853.  */
  854.  
  855. void
  856. TclExpandParseValue(pvPtr, needed)
  857.     register ParseValue *pvPtr;        /* Information about buffer that
  858.                      * must be expanded.  If the clientData
  859.                      * in the structure is non-zero, it
  860.                      * means that the current buffer is
  861.                      * dynamically allocated. */
  862.     int needed;                /* Minimum amount of additional space
  863.                      * to allocate. */
  864. {
  865.     int newSpace;
  866.     char *new;
  867.  
  868.     /*
  869.      * Either double the size of the buffer or add enough new space
  870.      * to meet the demand, whichever produces a larger new buffer.
  871.      */
  872.  
  873.     newSpace = (pvPtr->end - pvPtr->buffer) + 1;
  874.     if (newSpace < needed) {
  875.     newSpace += needed;
  876.     } else {
  877.     newSpace += newSpace;
  878.     }
  879.     new = (char *) ckalloc((unsigned) newSpace);
  880.  
  881.     /*
  882.      * Copy from old buffer to new, free old buffer if needed, and
  883.      * mark new buffer as malloc-ed.
  884.      */
  885.  
  886.     memcpy((VOID *) new, (VOID *) pvPtr->buffer,
  887.         (size_t) (pvPtr->next - pvPtr->buffer));
  888.     pvPtr->next = new + (pvPtr->next - pvPtr->buffer);
  889.     if (pvPtr->clientData != 0) {
  890.     ckfree(pvPtr->buffer);
  891.     }
  892.     pvPtr->buffer = new;
  893.     pvPtr->end = new + newSpace - 1;
  894.     pvPtr->clientData = (ClientData) 1;
  895. }
  896.  
  897. /*
  898.  *----------------------------------------------------------------------
  899.  *
  900.  * TclWordEnd --
  901.  *
  902.  *    Given a pointer into a Tcl command, find the end of the next
  903.  *    word of the command.
  904.  *
  905.  * Results:
  906.  *    The return value is a pointer to the last character that's part
  907.  *    of the word pointed to by "start".  If the word doesn't end
  908.  *    properly within the string then the return value is the address
  909.  *    of the null character at the end of the string.
  910.  *
  911.  * Side effects:
  912.  *    None.
  913.  *
  914.  *----------------------------------------------------------------------
  915.  */
  916.  
  917. char *
  918. TclWordEnd(start, nested, semiPtr)
  919.     char *start;        /* Beginning of a word of a Tcl command. */
  920.     int nested;            /* Zero means this is a top-level command.
  921.                  * One means this is a nested command (close
  922.                  * brace is a word terminator). */
  923.     int *semiPtr;        /* Set to 1 if word ends with a command-
  924.                  * terminating semi-colon, zero otherwise.
  925.                  * If NULL then ignored. */
  926. {
  927.     register char *p;
  928.     int count;
  929.  
  930.     if (semiPtr != NULL) {
  931.     *semiPtr = 0;
  932.     }
  933.  
  934.     /*
  935.      * Skip leading white space (backslash-newline must be treated like
  936.      * white-space, except that it better not be the last thing in the
  937.      * command).
  938.      */
  939.  
  940.     for (p = start; ; p++) {
  941.     if (isspace(UCHAR(*p))) {
  942.         continue;
  943.     }
  944.     if ((p[0] == '\\') && (p[1] == '\n')) {
  945.         if (p[2] == 0) {
  946.         return p+2;
  947.         }
  948.         continue;
  949.     }
  950.     break;
  951.     }
  952.  
  953.     /*
  954.      * Handle words beginning with a double-quote or a brace.
  955.      */
  956.  
  957.     if (*p == '"') {
  958.     p = QuoteEnd(p+1, '"');
  959.     if (*p == 0) {
  960.         return p;
  961.     }
  962.     p++;
  963.     } else if (*p == '{') {
  964.     int braces = 1;
  965.     while (braces != 0) {
  966.         p++;
  967.         while (*p == '\\') {
  968.         (void) Tcl_Backslash(p, &count);
  969.         p += count;
  970.         }
  971.         if (*p == '}') {
  972.         braces--;
  973.         } else if (*p == '{') {
  974.         braces++;
  975.         } else if (*p == 0) {
  976.         return p;
  977.         }
  978.     }
  979.     p++;
  980.     }
  981.  
  982.     /*
  983.      * Handle words that don't start with a brace or double-quote.
  984.      * This code is also invoked if the word starts with a brace or
  985.      * double-quote and there is garbage after the closing brace or
  986.      * quote.  This is an error as far as Tcl_Eval is concerned, but
  987.      * for here the garbage is treated as part of the word.
  988.      */
  989.  
  990.     while (1) {
  991.     if (*p == '[') {
  992.         for (p++; *p != ']'; p++) {
  993.         p = TclWordEnd(p, 1, (int *) NULL);
  994.         if (*p == 0) {
  995.             return p;
  996.         }
  997.         }
  998.         p++;
  999.     } else if (*p == '\\') {
  1000.         (void) Tcl_Backslash(p, &count);
  1001.         p += count;
  1002.         if ((*p == 0) && (count == 2) && (p[-1] == '\n')) {
  1003.         return p;
  1004.         }
  1005.     } else if (*p == '$') {
  1006.         p = VarNameEnd(p);
  1007.         if (*p == 0) {
  1008.         return p;
  1009.         }
  1010.         p++;
  1011.     } else if (*p == ';') {
  1012.         /*
  1013.          * Include the semi-colon in the word that is returned.
  1014.          */
  1015.  
  1016.         if (semiPtr != NULL) {
  1017.         *semiPtr = 1;
  1018.         }
  1019.         return p;
  1020.     } else if (isspace(UCHAR(*p))) {
  1021.         return p-1;
  1022.     } else if ((*p == ']') && nested) {
  1023.         return p-1;
  1024.     } else if (*p == 0) {
  1025.         if (nested) {
  1026.         /*
  1027.          * Nested commands can't end because of the end of the
  1028.          * string.
  1029.          */
  1030.         return p;
  1031.         }
  1032.         return p-1;
  1033.     } else {
  1034.         p++;
  1035.     }
  1036.     }
  1037. }
  1038.  
  1039. /*
  1040.  *----------------------------------------------------------------------
  1041.  *
  1042.  * QuoteEnd --
  1043.  *
  1044.  *    Given a pointer to a string that obeys the parsing conventions
  1045.  *    for quoted things in Tcl, find the end of that quoted thing.
  1046.  *    The actual thing may be a quoted argument or a parenthesized
  1047.  *    index name.
  1048.  *
  1049.  * Results:
  1050.  *    The return value is a pointer to the last character that is
  1051.  *    part of the quoted string (i.e the character that's equal to
  1052.  *    term).  If the quoted string doesn't terminate properly then
  1053.  *    the return value is a pointer to the null character at the
  1054.  *    end of the string.
  1055.  *
  1056.  * Side effects:
  1057.  *    None.
  1058.  *
  1059.  *----------------------------------------------------------------------
  1060.  */
  1061.  
  1062. static char *
  1063. QuoteEnd(string, term)
  1064.     char *string;        /* Pointer to character just after opening
  1065.                  * "quote". */
  1066.     int term;            /* This character will terminate the
  1067.                  * quoted string (e.g. '"' or ')'). */
  1068. {
  1069.     register char *p = string;
  1070.     int count;
  1071.  
  1072.     while (*p != term) {
  1073.     if (*p == '\\') {
  1074.         (void) Tcl_Backslash(p, &count);
  1075.         p += count;
  1076.     } else if (*p == '[') {
  1077.         for (p++; *p != ']'; p++) {
  1078.         p = TclWordEnd(p, 1, (int *) NULL);
  1079.         if (*p == 0) {
  1080.             return p;
  1081.         }
  1082.         }
  1083.         p++;
  1084.     } else if (*p == '$') {
  1085.         p = VarNameEnd(p);
  1086.         if (*p == 0) {
  1087.         return p;
  1088.         }
  1089.         p++;
  1090.     } else if (*p == 0) {
  1091.         return p;
  1092.     } else {
  1093.         p++;
  1094.     }
  1095.     }
  1096.     return p-1;
  1097. }
  1098.  
  1099. /*
  1100.  *----------------------------------------------------------------------
  1101.  *
  1102.  * VarNameEnd --
  1103.  *
  1104.  *    Given a pointer to a variable reference using $-notation, find
  1105.  *    the end of the variable name spec.
  1106.  *
  1107.  * Results:
  1108.  *    The return value is a pointer to the last character that
  1109.  *    is part of the variable name.  If the variable name doesn't
  1110.  *    terminate properly then the return value is a pointer to the
  1111.  *    null character at the end of the string.
  1112.  *
  1113.  * Side effects:
  1114.  *    None.
  1115.  *
  1116.  *----------------------------------------------------------------------
  1117.  */
  1118.  
  1119. static char *
  1120. VarNameEnd(string)
  1121.     char *string;        /* Pointer to dollar-sign character. */
  1122. {
  1123.     register char *p = string+1;
  1124.  
  1125.     if (*p == '{') {
  1126.     for (p++; (*p != '}') && (*p != 0); p++) {
  1127.         /* Empty loop body. */
  1128.     }
  1129.     return p;
  1130.     }
  1131.     while (isalnum(UCHAR(*p)) || (*p == '_')) {
  1132.     p++;
  1133.     }
  1134.     if ((*p == '(') && (p != string+1)) {
  1135.     return QuoteEnd(p+1, ')');
  1136.     }
  1137.     return p-1;
  1138. }
  1139.  
  1140. /*
  1141.  *----------------------------------------------------------------------
  1142.  *
  1143.  * Tcl_ParseVar --
  1144.  *
  1145.  *    Given a string starting with a $ sign, parse off a variable
  1146.  *    name and return its value.
  1147.  *
  1148.  * Results:
  1149.  *    The return value is the contents of the variable given by
  1150.  *    the leading characters of string.  If termPtr isn't NULL,
  1151.  *    *termPtr gets filled in with the address of the character
  1152.  *    just after the last one in the variable specifier.  If the
  1153.  *    variable doesn't exist, then the return value is NULL and
  1154.  *    an error message will be left in interp->result.
  1155.  *
  1156.  * Side effects:
  1157.  *    None.
  1158.  *
  1159.  *----------------------------------------------------------------------
  1160.  */
  1161.  
  1162. char *
  1163. Tcl_ParseVar(interp, string, termPtr)
  1164.     Tcl_Interp *interp;            /* Context for looking up variable. */
  1165.     register char *string;        /* String containing variable name.
  1166.                      * First character must be "$". */
  1167.     char **termPtr;            /* If non-NULL, points to word to fill
  1168.                      * in with character just after last
  1169.                      * one in the variable specifier. */
  1170.  
  1171. {
  1172.     char *name1, *name1End, c, *result;
  1173.     register char *name2;
  1174. #define NUM_CHARS 200
  1175.     char copyStorage[NUM_CHARS];
  1176.     ParseValue pv;
  1177.  
  1178.     /*
  1179.      * There are three cases:
  1180.      * 1. The $ sign is followed by an open curly brace.  Then the variable
  1181.      *    name is everything up to the next close curly brace, and the
  1182.      *    variable is a scalar variable.
  1183.      * 2. The $ sign is not followed by an open curly brace.  Then the
  1184.      *    variable name is everything up to the next character that isn't
  1185.      *    a letter, digit, or underscore.  If the following character is an
  1186.      *    open parenthesis, then the information between parentheses is
  1187.      *    the array element name, which can include any of the substitutions
  1188.      *    permissible between quotes.
  1189.      * 3. The $ sign is followed by something that isn't a letter, digit,
  1190.      *    or underscore:  in this case, there is no variable name, and "$"
  1191.      *    is returned.
  1192.      */
  1193.  
  1194.     name2 = NULL;
  1195.     string++;
  1196.     if (*string == '{') {
  1197.     string++;
  1198.     name1 = string;
  1199.     while (*string != '}') {
  1200.         if (*string == 0) {
  1201.         Tcl_SetResult(interp, "missing close-brace for variable name",
  1202.             TCL_STATIC);
  1203.         if (termPtr != 0) {
  1204.             *termPtr = string;
  1205.         }
  1206.         return NULL;
  1207.         }
  1208.         string++;
  1209.     }
  1210.     name1End = string;
  1211.     string++;
  1212.     } else {
  1213.     name1 = string;
  1214.     while (isalnum(UCHAR(*string)) || (*string == '_')) {
  1215.         string++;
  1216.     }
  1217.     if (string == name1) {
  1218.         if (termPtr != 0) {
  1219.         *termPtr = string;
  1220.         }
  1221.         return "$";
  1222.     }
  1223.     name1End = string;
  1224.     if (*string == '(') {
  1225.         char *end;
  1226.  
  1227.         /*
  1228.          * Perform substitutions on the array element name, just as
  1229.          * is done for quotes.
  1230.          */
  1231.  
  1232.         pv.buffer = pv.next = copyStorage;
  1233.         pv.end = copyStorage + NUM_CHARS - 1;
  1234.         pv.expandProc = TclExpandParseValue;
  1235.         pv.clientData = (ClientData) NULL;
  1236.         if (TclParseQuotes(interp, string+1, ')', 0, &end, &pv)
  1237.             != TCL_OK) {
  1238.         char msg[100];
  1239.         sprintf(msg, "\n    (parsing index for array \"%.*s\")",
  1240.             (int) (string-name1), name1);
  1241.         Tcl_AddErrorInfo(interp, msg);
  1242.         result = NULL;
  1243.         name2 = pv.buffer;
  1244.         if (termPtr != 0) {
  1245.             *termPtr = end;
  1246.         }
  1247.         goto done;
  1248.         }
  1249.         Tcl_ResetResult(interp);
  1250.         string = end;
  1251.         name2 = pv.buffer;
  1252.     }
  1253.     }
  1254.     if (termPtr != 0) {
  1255.     *termPtr = string;
  1256.     }
  1257.  
  1258.     if (((Interp *) interp)->noEval) {
  1259.     return "";
  1260.     }
  1261.     c = *name1End;
  1262.     *name1End = 0;
  1263.     result = Tcl_GetVar2(interp, name1, name2, TCL_LEAVE_ERR_MSG);
  1264.     *name1End = c;
  1265.  
  1266.     done:
  1267.     if ((name2 != NULL) && (pv.buffer != copyStorage)) {
  1268.     ckfree(pv.buffer);
  1269.     }
  1270.     return result;
  1271. }
  1272.  
  1273. /*
  1274.  *----------------------------------------------------------------------
  1275.  *
  1276.  * Tcl_CommandComplete --
  1277.  *
  1278.  *    Given a partial or complete Tcl command, this procedure
  1279.  *    determines whether the command is complete in the sense
  1280.  *    of having matched braces and quotes and brackets.
  1281.  *
  1282.  * Results:
  1283.  *    1 is returned if the command is complete, 0 otherwise.
  1284.  *
  1285.  * Side effects:
  1286.  *    None.
  1287.  *
  1288.  *----------------------------------------------------------------------
  1289.  */
  1290.  
  1291. int
  1292. Tcl_CommandComplete(cmd)
  1293.     char *cmd;            /* Command to check. */
  1294. {
  1295.     register char *p = cmd;
  1296.     int commentOK = 1;
  1297.  
  1298.     while (1) {
  1299.     while (isspace(UCHAR(*p))) {
  1300.         if (*p == '\n') {
  1301.         commentOK = 1;
  1302.         }
  1303.         p++;
  1304.     }
  1305.     if ((*p == '#') && commentOK) {
  1306.         do {
  1307.         p++;
  1308.         } while ((*p != '\n') && (*p != 0));
  1309.         continue;
  1310.     }
  1311.     if (*p == 0) {
  1312.         return 1;
  1313.     }
  1314.     p = TclWordEnd(p, 0, &commentOK);
  1315.     if (*p == 0) {
  1316.         return 0;
  1317.     }
  1318.     p++;
  1319.     }
  1320. }
  1321.